package com.zt.ztyh.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.ztyh.admin.mapper.FundsinfoMapper;
import com.zt.ztyh.model.config.IdWorker;
import com.zt.ztyh.model.dto.FundsDto;
import com.zt.ztyh.model.entity.Funds;
import com.zt.ztyh.admin.service.FundsService;
import com.zt.ztyh.admin.mapper.FundsMapper;
import com.zt.ztyh.model.entity.Fundsinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author 贾天旺
* @description 针对表【ztyh_funds】的数据库操作Service实现
* @createDate 2024-04-12 13:44:32
*/
@Service
public class FundsServiceImpl extends ServiceImpl<FundsMapper, Funds>
    implements FundsService{

    @Autowired
    private FundsMapper fundsMapper;

    @Autowired
    private FundsinfoMapper fundsinfoMapper;

    @Override
    public Page<Funds> findAllFunds(Integer page, Integer limit, FundsDto fundsDto) {

        if(ObjectUtil.isNotEmpty(fundsDto.getFname())){
            return this.fundsMapper.selectPage(new Page<Funds>(page,limit),new QueryWrapper<Funds>().like("fname",fundsDto.getFname()));
        }
        if(fundsDto.getRisk()!=null){

            return this.fundsMapper.selectPage(new Page<Funds>(page,limit),new QueryWrapper<Funds>().orderByDesc("risk"));
        }
        if(ObjectUtil.isNotEmpty(fundsDto.getCreateTimeBegin()) || ObjectUtil.isNotEmpty(fundsDto.getCreateTimeEnd())){

            return this.fundsMapper.selectPage(new Page<Funds>(page,limit),new QueryWrapper<Funds>().between("createTime",fundsDto.getCreateTimeBegin(),fundsDto.getCreateTimeEnd()));
        }
        if(ObjectUtil.isNotEmpty(fundsDto.getMouthrate())){

            return this.fundsMapper.selectPage(new Page<Funds>(page,limit),new QueryWrapper<Funds>().orderByDesc("mouthrate"));
        }
        if(ObjectUtil.isNotEmpty(fundsDto.getYearrate())){

            return this.fundsMapper.selectPage(new Page<Funds>(page,limit),new QueryWrapper<Funds>().orderByDesc("yearrate"));
        }

        return this.fundsMapper.selectPage(new Page<Funds>(page,limit),null);
    }

    @Override
    public void insertOrUpdate(FundsDto funds) {
        System.out.println("=======>"+funds);
        Funds funds1=new Funds();
        Fundsinfo fundsinfo=new Fundsinfo();
        if(ObjectUtil.isNotEmpty(funds.getFid())){
            //修改表
            funds1.setUpdatetime(new Date());
            funds1.setFname(funds.getFname());
            funds1.setMouthrate(funds.getMouthrate());
            funds1.setYearrate(funds.getYearrate());
            funds1.setRisk(funds.getRisk());
            funds1.setRemark(funds.getRemark());
            funds1.setFid(funds.getFid());
            System.out.println("修改完funds"+funds1);
            int i = this.fundsMapper.updateById(funds1);
            if(i>=1){
                //修改详情表
                fundsinfo.setId(funds.getFid());
                fundsinfo.setUpdatetime(new Date());
                fundsinfo.setRate(funds.getRate());
                fundsinfo.setFundsname(funds.getFname());
                fundsinfo.setDescription(funds.getDescription());
                fundsinfo.setTerm(funds.getTerm());
                fundsinfo.setTread(funds.getTread());
                fundsinfo.setRules(funds.getRules());
                fundsinfo.setShare(funds.getShare());
                fundsinfo.setRemark(funds.getRemark());
                System.out.println("修改完详情表"+fundsinfo);
                this.fundsinfoMapper.updateById(fundsinfo);
            }
        }else{
            IdWorker idWorker=new IdWorker();
            funds.setFid(idWorker.nextId());
            funds1.setFid(funds.getFid());
            funds1.setFname(funds.getFname());
            funds1.setRisk(funds.getRisk());
            funds1.setMouthrate(funds.getMouthrate());
            funds1.setYearrate(funds.getYearrate());
            funds1.setRemark(funds.getRemark());
            funds1.setCreatetime(new Date());
            funds1.setStatus(0);
            System.out.println("添加产品"+funds1);
            int insert = this.fundsMapper.insert(funds1);
            if(insert>=1){
                //详情表
                fundsinfo.setId(funds.getFid());
                fundsinfo.setFundsname(funds.getFname());
                fundsinfo.setTerm(funds.getTerm());
                fundsinfo.setTread(funds.getTread());
                fundsinfo.setRules(funds.getRules());
                fundsinfo.setShare(funds.getShare());
                fundsinfo.setRemark(funds.getRemark());
                fundsinfo.setCreatetime(new Date());
                fundsinfo.setDescription(funds.getDescription());
                fundsinfo.setRate(funds.getRate());
                System.out.println("添加详情"+fundsinfo);
                this.fundsinfoMapper.insert(fundsinfo);
            }
        }
    }
}




